﻿using Leo.Blog.BLL.SysModule;
using Leo.Blog.Common.JsonHelper;
using Leo.Blog.ViewModel.SysModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Leo.Blog.ViewModel;
using System.Data;
using Leo.Blog.Filter;

namespace Leo.Blog.Controllers
{
    public class AccountController : BaseController
    {
        // GET: Account
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetUserAccounts(string account, string name, string phone, int isenable=-1, int orgId=-1,
            int page = 1, int rows = 30)
        {
            int totalCount = 0;
            SysUserVM vm = new SysUserVM
            {
                Account=account,
                Name=name,
                TelePhone=phone,
                IsEnable=isenable
            };
            List<SysUserVM> listUsers = Sys_Account_B.GetUserAccounts(vm,orgId,page, rows, out totalCount);
            var obj = new
            {
                total = totalCount,
                rows = listUsers,
            };
            var toJson = JsonHelper.SerializeObject(obj);
            return Content(toJson);
        }

        public string ImportAccountData(string filePath)
        {
            List<SysUserVM> users = new List<SysUserVM>();
            StringBuilder sb = new StringBuilder();
            bool checkResult = Sys_Account_B.CheckImportData(filePath, users, ref sb);
            //校验通过直接保存
            if (checkResult)
            {
                bool saveResult= Sys_Account_B.SaveImportData(users);
                if (saveResult)
                {
                    ResCode resOK = new ResCode("1","OK");
                    return JsonHelper.SerializeObject(resOK);
                }
                else
                {
                    ResCode resSave = new ResCode("0", "保存数据库失败");
                    return JsonHelper.SerializeObject(resSave);
                }
            }
            ResCode res = new ResCode("0", sb.ToString());
            return JsonHelper.SerializeObject(res);
        }

        [AllowAccess]
        public ActionResult Export(string account, string name, string phone, int isenable, int orgId)
        {
            var exportSpource = this.GetExportData(account, name, phone, isenable, orgId);
            var dt = JsonHelper.DeserializeJsonToObject<DataTable>(JsonHelper.SerializeObject(exportSpource));

            var exportFileName = string.Concat(
                "Person",
                DateTime.Now.ToString("yyyyMMddHHmmss"),
                ".xlsx");

            return new ExportExcelResult
            {
                SheetName = "账号列表",
                FileName = exportFileName,
                ExportData = dt
            };
        }

        private List<SysUserVM> GetExportData(string account, string name, string phone, int isenable, int orgId)
        {
            SysUserVM vm = new SysUserVM
            {
                Account = account,
                Name = name,
                TelePhone = phone,
                IsEnable = isenable
            };
            List<SysUserVM> accountList = Sys_Account_B.GetExportUserAccounts(vm, orgId);
            return accountList;
        }
    }
}